package com.tjetc.view;

import java.util.Scanner;

import com.tjetc.domian.Admin;
import com.tjetc.domian.User;
import com.tjetc.portal.UserMainView;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.service.UserService;
import com.tjetc.usermanage.EventView;
import com.tjetc.utils.LogUtil;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

public class MainView {
    private static Scanner scanner = new Scanner(System.in);
    private static AdminService adminService = ClientProxy.getProxyInstance(AdminService.class);
    private static UserService userService = ClientProxy.getProxyInstance(UserService.class);

    public void start() {
        LogUtil.info("系统启动");
        while (true) {
            try {
                System.out.println("\n=== 社区管理系统 ===");
                System.out.println("1.管理员登录");
                System.out.println("2.用户登录");
                System.out.println("3.用户注册");
                System.out.println("0.退出系统");
                System.out.print("请选择功能编号(0-3): ");
                
                String input = scanner.nextLine().trim();
                if (!input.matches("[0-3]")) {
                    LogUtil.warn("用户输入无效: " + input);
                    System.out.println("输入无效，请输入0-3之间的数字！");
                    continue;
                }
                
                int choice = Integer.parseInt(input);
                switch (choice) {
                    case 1:
                        LogUtil.info("用户选择管理员登录");
                        adminLogin();
                        break;
                    case 2: 
                        LogUtil.info("用户选择用户登录");
                        userLogin();
                        break;
                    case 3:
                        LogUtil.info("用户选择用户注册");
                        userRegist();
                        break;
                    case 0:
                        LogUtil.info("用户选择退出系统");
                        System.out.println("感谢使用，再见！");
                        System.exit(0);
                    default:
                        LogUtil.warn("用户输入无效选项: " + choice);
                        System.out.println("输入有误,请重新输入!");
                }
            } catch (Exception e) {
                LogUtil.error("系统运行出错", e);
                System.out.println("发生错误：" + e.getMessage());
                scanner.nextLine(); // 清除输入缓冲
            }
        }
    }

    private void userRegist() {
        try {
            System.out.println("\n=== 用户注册 ===");
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine().trim();
            if (username.isEmpty()) {
                LogUtil.warn("用户注册时用户名为空");
                System.out.println("用户名不能为空！");
                return;
            }

            System.out.print("请输入密码: ");
            String password = scanner.nextLine().trim();
            if (password.isEmpty()) {
                LogUtil.warn("用户注册时密码为空");
                System.out.println("密码不能为空！");
                return;
            }

            System.out.print("请输入居民ID: ");
            String residentIdStr = scanner.nextLine().trim();
            if (!residentIdStr.matches("\\d+")) {
                LogUtil.warn("用户注册时居民ID格式错误: " + residentIdStr);
                System.out.println("居民ID必须是数字！");
                return;
            }
            int residentId = Integer.parseInt(residentIdStr);

            User user = new User(username, password);
            user.setResidentId(residentId);
            LogUtil.info("尝试注册新用户: " + username);
            Result result = userService.add(user);
            LogUtil.info("用户注册结果: " + result.getMsg());
            System.out.println(result.getMsg());
        } catch (Exception e) {
            LogUtil.error("用户注册过程发生错误", e);
            System.out.println("注册过程中发生错误：" + e.getMessage());
        }
    }

    private void userLogin() {
        try {
            System.out.println("\n=== 用户登录 ===");
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine().trim();
            if (username.isEmpty()) {
                LogUtil.warn("用户登录时用户名为空");
                System.out.println("用户名不能为空！");
                return;
            }

            System.out.print("请输入密码: ");
            String password = scanner.nextLine().trim();
            if (password.isEmpty()) {
                LogUtil.warn("用户登录时密码为空");
                System.out.println("密码不能为空！");
                return;
            }

            User user = new User(username, password);
            LogUtil.info("用户尝试登录: " + username);
            Result result = userService.login(user);
            LogUtil.info("用户登录结果: " + result.getMsg());
            System.out.println(result.getMsg());
            
            if (result.getCode() == 200) {
                AppData.loginUser = (User) result.getData();
                User user1 = (User) result.getData();
                AppData.setCurrentResidentId(user1.getResidentId());
                EventView.current_user = username;
                LogUtil.info("用户登录成功: " + username);
                new UserMainView().start();
            }
        } catch (Exception e) {
            LogUtil.error("用户登录过程发生错误", e);
            System.out.println("登录过程中发生错误：" + e.getMessage());
        }
    }

    private void adminLogin() {
        try {
            System.out.println("\n=== 管理员登录 ===");
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine().trim();
            if (username.isEmpty()) {
                LogUtil.warn("管理员登录时用户名为空");
                System.out.println("用户名不能为空！");
                return;
            }

            System.out.print("请输入密码: ");
            String password = scanner.nextLine().trim();
            if (password.isEmpty()) {
                LogUtil.warn("管理员登录时密码为空");
                System.out.println("密码不能为空！");
                return;
            }

            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            LogUtil.info("管理员尝试登录: " + username);
            Result result = adminService.login(admin);
            LogUtil.info("管理员登录结果: " + result.getMsg());
            System.out.println(result.getMsg());
            
            if (result.getCode() == 200) {
                AppData.loginAdmin = (Admin) result.getData();
                LogUtil.info("管理员登录成功: " + username);
                new AdminMainView().start();
            }
        } catch (Exception e) {
            LogUtil.error("管理员登录过程发生错误", e);
            System.out.println("登录过程中发生错误：" + e.getMessage());
        }
    }
}
